package com.kchouse.mine;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.kchouse.HomepageFragment;
import com.kchouse.MainActivity;
import com.kchouse.MineFragment;
import com.kchouse.R;
import com.kchouse.common.API;
import com.kchouse.common.BaseActivity;
import com.kchouse.common.MyHttpCycleContext;
import com.kchouse.common.SharedPreferencesUtil;
import com.kchouse.common.ToastUtils;

import cn.finalteam.okhttpfinal.BaseHttpRequestCallback;
import cn.finalteam.okhttpfinal.HttpRequest;
import cn.finalteam.okhttpfinal.RequestParams;

public class MineSetActivity extends BaseActivity implements View.OnClickListener {

    private MineSetActivity mContent;
    private TextView tv_title;
    private ImageView iv_back;
    private View ll_loginout;
    private Intent intent;
    private View ll_alterps;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mine_set);

        mContent = this;
        intiData();
        intiView();

    }

    private void intiData() {
        intent = getIntent();
    }

    private void intiView() {
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setText("设置");
        iv_back = (ImageView) findViewById(R.id.iv_back);
        iv_back.setOnClickListener(this);
        ll_loginout = findViewById(R.id.ll_loginout);
        ll_loginout.setOnClickListener(this);
        ll_alterps = findViewById(R.id.ll_alterps);
        ll_alterps.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.ll_loginout:
                checkloginout();
                break;
            case R.id.ll_alterps://修改密码
                intent.setClass(mContent,AlterpsActivity.class);
                startActivity(intent);
                break;
        }
    }

    /**
     * 确定身份（登录接口未通）
     */
    private void checkloginout() {
        // 创建构建器
        AlertDialog.Builder builder = new AlertDialog.Builder(mContent);
        // 设置参数
        builder.setTitle("是否退出登录").setPositiveButton("确定", new DialogInterface.OnClickListener() {// 积极

            @Override
            public void onClick(DialogInterface dialog,
                                int which) {
                loginout();
            }
        }).setNegativeButton("取消", null);
        builder.create().show();
    }

    /**
     * 退出登录
     */
    private void loginout() {
        String url = API.PostLogout + MainActivity.userid;
        RequestParams params = new RequestParams(new MyHttpCycleContext());//请求参数
        params.addHeader("token", MainActivity.token);//添加header信息
        params.addFormDataPart("userId", MainActivity.userid);

        HttpRequest.post(url, params, new BaseHttpRequestCallback<String>() {

            //请求网络前
            @Override
            public void onStart() {
                showdialog();
            }

            @Override
            protected void onSuccess(String str) {
                Log.e("==", str);
                JSONObject json = JSONObject.parseObject(str);
                if (json.getBoolean("successful")) {
//                    Log.e("==", json.getString("data"));
                    SharedPreferencesUtil.setParam(mContent, "token", "");//保存用户token
                    SharedPreferencesUtil.setParam(mContent, "mobile", "");//保存用户电话
                    SharedPreferencesUtil.setParam(mContent, "userid", "");//保存用户id
                    intent.setClass(mContent, LoginPhoneActivity.class);
                    startActivity(intent);

                    Intent mIntent = new Intent("LoginAction");
                    //发送广播
                    sendBroadcast(mIntent);
                    finish();
                } else {
                    String datastr = json.getString("data");
                    JSONObject data = JSONObject.parseObject(datastr);
//                    if(data.getString("code").equals("401")||data.getString("code").equals("400"))
//                    {
//                        intent.setClass(mContent, LoginPhoneActivity.class);
//                        startActivity(intent);
//                        finish();
//                    }else {
//                        ToastUtils.getInstanc(mContent).showToast(data.getString("msg"));
//                    }
                }
            }

            //请求失败（服务返回非法JSON、服务器异常、网络异常）
            @Override
            public void onFailure(int errorCode, String msg) {
//                toast("网络异常~，请检查你的网络是否连接后再试");
                Log.e("==", msg);
            }

            //请求网络结束
            @Override
            public void onFinish() {
                dismissdialog();
            }
        });
    }


}
